//
//  SAUpdateShopPriceCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/29.
//

#import "SAUpdateShopPriceCell.h"

@interface SAUpdateShopPriceCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIView *line;

@end

@implementation SAUpdateShopPriceCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self initUI];
    }
    return self;
}

-(void)refreshLeftTitle:(NSString *)leftTitle RightTitle:(NSString *)rightTitle Type:(NSInteger)type {
    self.titleLab.text = leftTitle;
    self.rightTf.text = rightTitle;
    self.rightTf.userInteractionEnabled = (type == 0);
    self.rightTf.keyboardType = UIKeyboardTypeDecimalPad;
    if ([leftTitle isEqualToString:@"直接调价"]) {
        self.rightTf.placeholder = @"";
    }else if ([leftTitle isEqualToString:@"统一加减"]) {
        self.rightTf.placeholder = @"填\"-8\"表示在原价格基础上减8";
        self.rightTf.keyboardType = UIKeyboardTypeDefault;
    }else if ([leftTitle isEqualToString:@"统一乘"]) {
        self.rightTf.placeholder = @"填\"0.8\"表示在原价基础上打8折";
    }else {
        self.rightTf.placeholder = [NSString stringWithFormat:@"请输入%@",leftTitle];
    }
}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (textField.text.length > 10 && string.length > 0) {
        return NO;
    }
    if ([string isEqualToString:@"-"]) {
        if ([self.titleLab.text isEqualToString:@"统一加减"] && textField.text.length == 0) {
            return YES;
        }else {
            return NO;
        }
    }
    BOOL isHaveDian =NO;
    if([textField.text containsString:@"."]) {
        isHaveDian =YES;
    }
    if(string.length > 0) {
        //当前输入的字符
        unichar single = [string characterAtIndex:0];
        // 不能输入.0-9以外的字符
        if(!((single >='0'&& single <='9') || single =='.')){
            return NO;
        }
        // 只能有一个小数点
        if(isHaveDian && single =='.') {
            return NO;
        }
        // 如果第一位是.则前面加上0.
        if((textField.text.length==0) && (single =='.')) {
            textField.text=@"0";
        }
        // 如果第一位是0则后面必须输入点，否则不能输入。
        if([textField.text hasPrefix:@"0"]) {
            if(textField.text.length>1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1,1)];
                if(![secondStr isEqualToString:@"."]) {
                    return NO;
                }
            }else{
                if(![string isEqualToString:@"."]) {
                    return NO;
                }
            }
        }
        // 小数点后最多能输入两位
        if(isHaveDian && ![self.titleLab.text isEqualToString:@"统一乘"]) {
            NSRange ran = [textField.text rangeOfString:@"."];
            if(range.location > ran.location) {
                if([textField.text pathExtension].length>1) {
                    return NO;
                }
            }
        }
    }
    return YES;
}


-(void)initUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.rightTf];
    [self.contentView addSubview:self.line];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(30, 0, 100, 30)];
        _titleLab.textColor = kBlackColor;
        _titleLab.text = @"库存上限";
        _titleLab.font = kBFont(13);
    }
    return _titleLab;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(0, 30, kBaseScreenWidth, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UITextField *)rightTf {
    if (!_rightTf) {
        _rightTf = [[UITextField alloc] initWithFrame:SARectMake(150, 0, 202, 30)];
        _rightTf.font = kFont(14);
        _rightTf.textColor = kGrayColor;
        _rightTf.textAlignment = NSTextAlignmentRight;
        _rightTf.keyboardType = UIKeyboardTypeDecimalPad;
        _rightTf.delegate = self;
    }
    return _rightTf;
}

@end
